package com.shop.front.service.system;

import com.shop.domain.system.Banner;
import com.shop.domain.system.Version;
import com.shop.front.consts.FrontConst;
import com.shop.front.dao.system.*;
import com.shop.front.property.StaticConstProps;
import com.shop.front.property.WxpayConstant;
import com.shop.front.vo.system.SystemCitySimpleVo;
import com.shop.common.enums.System.NewSystemConfigEnum;
import com.shop.domain.pay.PayConfig;
import com.shop.domain.system.NewSystemConfig;
import com.shop.domain.system.SystemGroupData;
import com.shop.front.dao.pay.PayConfigDao;
import com.shop.web.cache.RedisCache;
import com.shop.web.response.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@Transactional
public class SystemService {

    @Autowired
    private SystemCitySimpleDao systemCitySimpleDao;
    @Autowired
    private SystemGroupDataDao systemGroupDataDao;
    @Autowired
    private PayConfigDao payConfigDao;
    @Autowired
    private NewSystemConfigDao newSystemConfigDao;
    @Autowired
    private BannerDao bannerDao;
    @Autowired
    VersionDao versionDao;

    @Autowired
    RedisCache redisCache;

    @Autowired
    SysConfigDao sysConfigDao;

    public BaseResponse findCitySimple() {
        List<SystemCitySimpleVo> list = systemCitySimpleDao.findCitySimple();
        return new BaseResponse(list);
    }

    public List<SystemGroupData> findSystemGroupData(String configName) {
        List<SystemGroupData> list = systemGroupDataDao.findByConfigName(configName);
        return list;
    }

    public List<PayConfig> findPayConf() {
        return payConfigDao.findAll();
    }


    public NewSystemConfig getSystemConfig(NewSystemConfigEnum newSystemConfigEnum){
        return newSystemConfigDao.findByCode(newSystemConfigEnum.getCategory(),newSystemConfigEnum.getCode());
    }


    public List<NewSystemConfig> getSystemConfigList(NewSystemConfigEnum newSystemConfigEnum){
        return newSystemConfigDao.findByCategoryList(newSystemConfigEnum.getCategory());
    }


    public List<Banner> getBannerList() {
        return bannerDao.findByCategoryList(0);
    }

    /**
     * 获得最新app版本
     *
     * @return
     */
    public BaseResponse getLastVersion() {

        //获取版本默认安卓
        Version android = versionDao.getLastVersion("android");
        return new BaseResponse(android);
    }


    public String getSysConfig(String key) {

        Object obj =  redisCache.get(FrontConst.SYSTEM_CONFIG_KEY+key);
        if(obj != null){
            return obj.toString();
        }
        String value = sysConfigDao.findValueByKeyName(key);
        redisCache.set(FrontConst.SYSTEM_CONFIG_KEY+key,value);
        return value;
    }


    public BaseResponse shareWxAppid() {
        return new BaseResponse(StaticConstProps.wxAppId);
    }
}
